<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class skim extends CI_Controller {

	function __construct(){
            parent::__construct();
            $this->load->library(array('session','form_validation'));
            $this->load->helper(array('form', 'url'));
            $this->admin_id = $this->session->userdata('admin_id');
            $this->admin_username = $this->session->userdata('admin_username');
            $this->load->model('program_model');
            $this->load->model('skim_model');
            $this->load->model('admin_model');
            $this->load->model('penelitian_model');
        }
        
        public function authorize()
        {
            if($this->admin_id && $this->admin_model->get_admin($this->admin_id)->num_rows() > 0)return true;
            else
            {
                redirect('admin/login');
                return false;
            }
        }
        
	public function index()
	{
            $this->skim_list();
	}
        
        public function skim_list($tahun_id=-1,$program_id=-1)
        {
            $result = $this->authorize();
            if($result == false)return;
            $yes = 0;
            $cek1 = $this->program_model->get_tahun_by_id($tahun_id);
            $cek2 = $this->program_model->get_prog_from_id($program_id);            
            if($cek1->num_rows() > 0 && $cek2->num_rows() > 0)
            {
                $program_list = $this->program_model->get_program_by_tahun($tahun_id)->result_array();
                foreach($program_list as $row)
                {
                    if($row['PROGRAM_ID'] == $program_id)
                    {
                        $yes = 1;
                        $resultx = $this->skim_model->get_skim_byprogram($program_id)->result_array();
                        $data['program_list'] = $program_list;
                        break;
                    }
                }
            }
            if($yes == 0)
            {
                $tahun_id=-1;
                $program_id=-1;
                $resultx = $this->skim_model->get_skim()->result_array();
                $data['program_list'] = array();
            }
            
            foreach($resultx as &$row)
            {
                $result2 = $this->program_model->get_prog_from_id($row['PROGRAM_ID'])->row();
                $row['PROGRAM_NAMA'] = $result2->PROGRAM_NAMA;
            }
            $data['skim_list'] = $resultx;
            $data['list_tahun'] = $this->program_model->get_list_tahun()->result_array();
            $data['tahun_id'] = $tahun_id;
            $data['program_id'] = $program_id;
            $this->load->view('template/header'); 
            $this->load->view('skim/list',$data);
            $this->load->view('admin/sidebar');
            $this->load->view('template/footer');            
        }

        public function insert($step=1)
        {
            $result = $this->authorize();
            if($result == false)return;
            
            if($step == 1)
            {
                $data['list_tahun'] = $this->program_model->get_list_tahun()->result_array();
                $this->load->view('template/header'); 
                $this->load->view('skim/insert_step1',$data);
                $this->load->view('admin/sidebar');
                $this->load->view('template/footer');
                return;
            }
            else if($step == 2)
            {
                $program_id = $this->input->post('program_id');
                $result = $this->program_model->get_prog_from_id($program_id);
                if($program_id && $result->num_rows() > 0)
                {
                    $data['submit_data']=array(
                        'program_id' => $program_id,
                        'program_name' => $result->row()->PROGRAM_NAMA,
                        'skim_kode' => "",
                        'skim_nama' => "",
                        'skim_max_dana' => "",
                        'skim_max_anggota' => ""
                    );
                    $data['list_bidang_pen'] = $this->skim_model->get_bidang_penelitian()->result_array();
                    
                    $this->load->view('template/header'); 
                    $this->load->view('skim/insert_step2',$data);
                    $this->load->view('admin/sidebar');
                    $this->load->view('template/footer');                   
                }
                else
                {
                    $this->insert();
                }
                return;
            }
            else if($step == 3)
            {
                $program_id = $this->input->post('program_id');
                $program_name = $this->input->post('program_name');
                $skim_kode = $this->input->post('skim_kode');
                $skim_nama = $this->input->post('skim_nama');
                $skim_max_dana = $this->input->post('skim_max_dana');
                $skim_max_anggota = $this->input->post('skim_max_anggota');
                
                if($program_id == NULL || $program_id == false)
                {
                    $this->insert();
                    return;
                }
                
                $data['list_bidang_pen'] = $this->skim_model->get_bidang_penelitian()->result_array();
                    
                $data['submit_data']=array(
                    'program_id' => $program_id,
                    'program_name' => $program_name,
                    'skim_kode' => $skim_kode,
                    'skim_nama' => $skim_nama,
                    'skim_max_dana' => $skim_max_dana,
                    'skim_max_anggota' => $skim_max_anggota
                );
                
                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
                $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 10 karakter");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

                $this->form_validation->set_rules('program_name', 'Nama Program', 'trim|required');
                $this->form_validation->set_rules('skim_kode', 'Kode Skim', 'trim|max_length[10]|required');
                $this->form_validation->set_rules('skim_nama', 'Nama Skim', 'trim|required');
                $this->form_validation->set_rules('skim_max_dana', 'Maksimal dana', 'trim|numeric|required');
                $this->form_validation->set_rules('skim_max_anggota', 'Jumlah Anggota Maksimal', 'trim|numeric|required');


                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data);
                    $this->load->view('skim/insert_step2', $data);
                    $this->load->view('admin/sidebar');
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->skim_model->insert_skim($program_id,$skim_kode,$skim_nama,$skim_max_dana,$skim_max_anggota,"null");
                    $last_id = $this->db->insert_id();
                    
                    
                    $this->load->library('upload');  
                    $config['upload_path'] = './static/docs/skim'; 
                    $config['allowed_types'] = 'pdf|doc|docx';  
                    $config['max_size']  = '0';
                    $config['file_name'] = $last_id;
                    $config['overwrite']  = TRUE;
                    $this->upload->initialize($config);
                    $upload = $this->upload->do_upload();
                    if($upload)
                    {
                        $data = $this->upload->data();
                        $path = $data['file_name'];
                        $this->skim_model->insert_panduan_path($last_id,$path);
                    }
                    else
                    {
                        //$error = array('error' => $this->upload->display_errors());
                        //var_dump($error);
                    }
                    $bid_penelitian = $this->skim_model->get_bidang_penelitian()->result_array();
                    foreach($bid_penelitian as $row)
                    {
                        if($this->input->post('bidang_pen_'.$row['BIDANG_PEN_ID']))
                        {
                            $this->skim_model->insert_bidang_penelitian_skim($last_id,$row['BIDANG_PEN_ID']);
                        }
                    }
                    $this->index();
                    /* TBA : Step 3 -> tambah pengumuman, jadwal & syarat 
                    $this->load->view('template/header', $data);
                    $this->load->view('skim/insert_step3', $data);
                    $this->load->view('admin/sidebar');
                    $this->load->view('template/footer', $data);
                     */
                }
                return;
            }
            else if($step == 4)
            {
                
            }
            $this->index();
            return;
        }
        
        public function do_update()
        {
            $skim_id = $this->input->post('skim_id');
            $program_id = $this->input->post('program_id');
            $program_name = $this->input->post('program_name');
            $skim_kode = $this->input->post('skim_kode');
            $skim_nama = $this->input->post('skim_nama');
            $skim_max_dana = $this->input->post('skim_max_dana');
            $skim_max_anggota = $this->input->post('skim_max_anggota');

            $data['list_bidang_pen'] = $this->skim_model->get_bidang_penelitian()->result_array();

            $data['submit_data']=array(
                'program_id' => $program_id,
                'program_name' => $program_name,
                'skim_kode' => $skim_kode,
                'skim_nama' => $skim_nama,
                'skim_max_dana' => $skim_max_dana,
                'skim_max_anggota' => $skim_max_anggota
            );

            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 10 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

            $this->form_validation->set_rules('program_name', 'Nama Program', 'trim|required');
            $this->form_validation->set_rules('skim_kode', 'Kode Skim', 'trim|max_length[10]|required');
            $this->form_validation->set_rules('skim_nama', 'Nama Skim', 'trim|required');
            $this->form_validation->set_rules('skim_max_dana', 'Maksimal dana', 'trim|numeric|required');
            $this->form_validation->set_rules('skim_max_anggota', 'Jumlah Anggota Maksimal', 'trim|numeric|required');


            if ($this->form_validation->run() == FALSE)
            {    
                $this->load->view('skim/update', $data);
            }
            else
            {
                $this->skim_model->update_skim($skim_id,$program_id,$skim_kode,$skim_nama,$skim_max_dana,$skim_max_anggota);
                $last_id = $skim_id;

                $this->load->library('upload');  
                $config['upload_path'] = './static/docs/skim'; 
                $config['allowed_types'] = 'pdf|doc|docx';  
                $config['max_size']  = '0';
                $config['file_name'] = $last_id;
                $config['overwrite']  = TRUE;
                $this->upload->initialize($config);
                $upload = $this->upload->do_upload();
                if($upload)
                {
                    $data = $this->upload->data();
                    $path = $data['file_name'];
                    $this->skim_model->insert_panduan_path($last_id,$path);
                }
                else
                {
                    //$error = array('error' => $this->upload->display_errors());
                    //var_dump($error);
                }
                $bid_penelitian = $this->skim_model->get_bidang_penelitian()->result_array();
                foreach($bid_penelitian as $row)
                {
                    if($this->input->post('bidang_pen_'.$row['BIDANG_PEN_ID']))
                    {
                        $this->skim_model->insert_bidang_penelitian_skim($last_id,$row['BIDANG_PEN_ID']);
                    }
                    else
                    {
                        $this->skim_model->delete_bidang_penelitian_skim($last_id,$row['BIDANG_PEN_ID']);
                    }
                }
                $this->load->view('skim/update_success');
            }          
        }
        
        public function update($skim_id)
        {
            $query = $this->skim_model->get_skim_from_id($skim_id);
            if($query->num_rows() > 0)
            {
                $arr = $query->result_array();
                $query2 = $this->program_model->get_prog_from_id($arr[0]['PROGRAM_ID']);
                $query3 = $this->skim_model->get_bidang_penelitian_skim($skim_id)->result_array();
            }
            if($query->num_rows() > 0 && $query2->num_rows() > 0)
            {
                $arr[0]['PROGRAM_NAMA'] = $query2->row()->PROGRAM_NAMA;
                $data['skim_info'] = $arr[0];
                foreach($query3 as &$row)
                {
                    $row['BIDANG_PEN_NAMA'] = $this->skim_model->get_bidang_penelitian_by_id($row['BIDANG_PEN_ID'])->row()->BIDANG_PEN_NAMA;
                }
                $data['bid_pen_skim_list'] = $query3;
                $data['list_bidang_pen'] = $this->skim_model->get_bidang_penelitian()->result_array();
                $this->load->view('skim/update', $data);
            }
            else
            {
                $this->index();
            }
        }
        
        public function delete($skim_id)
        {
            $this->skim_model->delete_skim($skim_id);
            $this->index();
            return;
        }

        public function detail($skim_id)
        {
            $result = $this->authorize();
            if($result == false)return;
            $query = $this->skim_model->get_skim_from_id($skim_id);
            if($query->num_rows() > 0)
            {
                $arr = $query->result_array();
                $query2 = $this->program_model->get_prog_from_id($arr[0]['PROGRAM_ID']);
                $query3 = $this->skim_model->get_bidang_penelitian_skim($skim_id)->result_array();
                $query4 = $this->penelitian_model->get_penelitian_by_skim($skim_id)->result_array();
                $query5 = $this->skim_model->get_persyaratan_skim_by_skim_id($skim_id)->result_array();
                $query6 = $this->skim_model->get_pengumuman_skim_penelitian_by_skim_id($skim_id)->result_array();
            }
            if($query->num_rows() > 0 && $query2->num_rows() > 0)
            {
                $arr[0]['PROGRAM_NAMA'] = $query2->row()->PROGRAM_NAMA;
                $data['skim_info'] = $arr[0];
                foreach($query3 as &$row)
                {
                    $row['BIDANG_PEN_NAMA'] = $this->skim_model->get_bidang_penelitian_by_id($row['BIDANG_PEN_ID'])->row()->BIDANG_PEN_NAMA;
                }
                $data['bid_pen_skim_list'] = $query3;
                $data['penelitian_list'] = $query4;
                $data['persyaratan_skim_list'] = $query5;
                $data['pengumuman_skim_list'] = $query6;
                $this->load->view('template/header', $data);
                $this->load->view('skim/detail', $data);
                $this->load->view('admin/sidebar');
                $this->load->view('template/footer', $data);
            }
            else
            {
                $this->index();
            }
        }

        public function add_pengumuman($skim_id)
        {
            $pengumuman_judul = $this->input->post('pengumuman_judul');
            $pengumuman_deskripsi = $this->input->post('pengumuman_deskripsi');
            
            $data['skim_id'] = $skim_id;
            $data['submit_data']=array(
                'pengumuman_judul' => $pengumuman_judul,
                'pengumuman_deskripsi' => $pengumuman_deskripsi
            );

            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 10 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

            $this->form_validation->set_rules('pengumuman_judul', 'Judul Pengumuman', 'trim|required');
            $this->form_validation->set_rules('pengumuman_deskripsi', 'Deskripsi Pengumuman', 'trim|required');


            if ($this->form_validation->run() == FALSE)
            {    
                $this->load->view('skim/add_pengumuman',$data);
            }
            else
            {
                $this->skim_model->insert_pengumuman_skim_penelitian($skim_id,$pengumuman_judul,$pengumuman_deskripsi);
                $this->load->view('skim/update_success');
            }
        }
        
        public function add_syarat($skim_id)
        {
            $syarat_deskripsi = $this->input->post('syarat_deskripsi');
            
            $data['skim_id'] = $skim_id;
            $data['submit_data']=array(
                'syarat_deskripsi' => $syarat_deskripsi
            );

            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 10 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

            $this->form_validation->set_rules('syarat_deskripsi', 'Deskripsi Syarat', 'trim|required');


            if ($this->form_validation->run() == FALSE)
            {    
                $this->load->view('skim/add_syarat',$data);
            }
            else
            {
                $this->skim_model->insert_persyaratan_skim($skim_id,$syarat_deskripsi);
                $this->load->view('skim/update_success');
            }
        }
        
        public function delete_syarat($skim_id,$syarat_id)
        {
            $this->skim_model->delete_persyaratan_skim($syarat_id);
            redirect('skim/detail/'.$skim_id);
        }
        
        public function get_skim()
	{       $program_id = $this->input->post('prog');
                $q = $this->skim_model->get_skim_byprogram($program_id);
		echo "<option value=\"0\" >Pilih Program</option>";
                foreach ($q->result() as $r){
                    echo "<option value=\"$r->SKIM_ID\" >$r->SKIM_NAMA</option>";
                }
            //echo $program_id;
        }
        
        public function get_program()
	{       
            $tahun_id = $this->input->post('tahun');
            $q = $this->skim_model->get_program_bytahun($tahun_id);
            echo "<option value=\"0\" >Pilih tahun</option>";
            foreach ($q->result() as $r){
                echo "<option value=\"$r->PROGRAM_ID\" >$r->PROGRAM_NAMA</option>";
            }
            //echo $program_id;
        }
        
        public function bidang_penelitian()
        {
            $result = $this->skim_model->get_bidang_penelitian()->result_array();
            $data['list_bidang_penelitian'] = $result;
            $this->load->view('template/header', $data);
            $this->load->view('skim/list_bidang_penelitian', $data);
            $this->load->view('admin/sidebar');
            $this->load->view('template/footer', $data);           
        }
        
        public function insert_bidang_penelitian()
        {
            $new_bidang = $this->input->post('new_bidang');
            $new_bidang_id = $this->input->post('new_bidang_id');
            if($new_bidang && $new_bidang_id)
            {
                $this->skim_model->insert_bidang_penelitian($new_bidang_id,$new_bidang);
            }
            $this->bidang_penelitian();
        }
        
        public function delete_bidang_penelitian($id_bidang_penelitian)
        {
            $this->skim_model->delete_bidang_penelitian($id_bidang_penelitian);
            $this->bidang_penelitian();
        }
}
